600C - Make Palindrome - CodeForces Solution


constructive algorithms greedy strings *1800

Please click on ads to support us..

Python Code:

def solution(s):
    d = dict()

    for ch in s:
        if ch in d:
            d[ch] += 1
        else:
            d[ch] = 1
    
    odd_letters = []
    for ch in d:
        if d[ch] % 2 != 0:
            odd_letters.append(ch)

        if len(odd_letters) == 0:
        pass
    else:
        odd_letters.sort(key=lambda ch : ord(ch))

        n = len(odd_letters)
        
        for i in range(n//2):
            d[odd_letters[i]] += 1
            d[odd_letters[n-i-1]] -= 1

                
    
    parts = []
    for ch in sorted(d.keys(), key=lambda ch: ord(ch)):
        parts.append(ch * (d[ch]//2))
    
    rparts = reversed(parts)

    for ch in d.keys():
        if d[ch] % 2 != 0:
            parts.append(ch)
            break

    parts.extend(rparts)
    return ''.join(parts)    


def test():
    pass


def main():
    test()
    s = input()
    print(solution(s))


main()

C++ Code:

 

 

#include<bits/stdc++.h>

using namespace std;

 

typedef long long int   ll;



#define pi(x) cout<<x;

#define ps(x) cout<<x<<" ";

#define pnl(x) cout<<x<<"\n";

#define for0(n) for(i=0;i<n;i++)

#define for1(n) for(i=1;i<=n;i++)

#define m(x) memset(x,0,sizeof x);

#define nl cout<<"\n";

#define mp make_pair

#define pb push_back

#define fr first

#define se second

#define Inf 1e16

#define MOD 1e9+7

vector<ll>a[100000];

ll dp[100000];

ll xr[100000];

ll countx=0,xx=0;

void dfs(ll x,ll p){

      xr[x]=dp[x];

      for(auto dd:a[x]){

          if(p!=dd){

              dfs(dd,x);

              xr[x]^=xr[dd];

          }

      }

      if(xr[x]==xx){

          xr[x]=0;

          countx+=(p!=0);

      }

}



int main(){

 ios_base::sync_with_stdio(false);

 cin.tie(NULL);

 cout.tie(NULL);

ll test,h,p,i,j,xy,flag=0,n,u,count,d,o1=0,o2=0,s,e,l,r,x,y,m,z,max1,x1,y1,k,x2,y2,z1,z2,sum,min1;



string a;

cin>>a;

ll dp[26];

ll od[26];

memset(dp,0,sizeof dp);

memset(od,0,sizeof od);

for(i=0;i<a.size();i++){

    dp[a[i]-'a']++;

    od[a[i]-'a']++;

    od[a[i]-'a']%=2;

}

for(i=0;i<26;i++){

    if(od[i]){

    for(j=25;j>i;j--){

        if(od[j]){

            dp[i]++;

            od[i]--;

            od[j]--;

            break;

        }

     }

    }

}

n=a.size();

count=0;

for(i=0;i<n-i-1;i++){

    while(count<26&&dp[count]<2){

        count++;

    }

    if(count>=26){

        break;

    }

    a[i]=(count+'a');

    a[n-i-1]=(count+'a');

    dp[count]-=2;

}

if(n%2){

    for(i=0;i<26;i++){

        if(od[i]){

            a[n/2]=(i+'a');

        }

    }

}

cout<<a<<"\n";

return 0;

}


Comments

Submit
0 Comments
More Questions

152. Maximum Product Subarray
337. House Robber III
869. Reordered Power of 2
1593C - Save More Mice
1217. Minimum Cost to Move Chips to The Same Position
347. Top K Frequent Elements
1503. Last Moment Before All Ants Fall Out of a Plank
430. Flatten a Multilevel Doubly Linked List
1290. Convert Binary Number in a Linked List to Integer
1525. Number of Good Ways to Split a String
72. Edit Distance
563. Binary Tree Tilt
1306. Jump Game III
236. Lowest Common Ancestor of a Binary Tree
790. Domino and Tromino Tiling
878. Nth Magical Number
2099. Find Subsequence of Length K With the Largest Sum
1608A - Find Array
416. Partition Equal Subset Sum
1446. Consecutive Characters
1618A - Polycarp and Sums of Subsequences
1618B - Missing Bigram
938. Range Sum of BST
147. Insertion Sort List
310. Minimum Height Trees
2110. Number of Smooth Descent Periods of a Stock
2109. Adding Spaces to a String
2108. Find First Palindromic String in the Array
394. Decode String
902. Numbers At Most N Given Digit Set